package info.techgeek.labs.collectionsort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortEmployeeList {

	public static void main(String[] args) {
		Employee emp1 = new Employee(1, "Dipen", "Khadka");
		Employee emp2 = new Employee(2, "John", "Rotela");
		Employee emp3 = new Employee(3, "Shark", "Anderson");
		Employee emp4 = new Employee(4, "Suman", "Bhatt");

		List<Employee> employeeList = new ArrayList<Employee>();
		employeeList.add(emp1);
		employeeList.add(emp2);
		employeeList.add(emp3);
		employeeList.add(emp4);
		System.out.println("Sort Based on FirstName ");
		// sort by First name
		Collections.sort(employeeList, new Comparator() {
			@Override
			public int compare(Object obj1, Object obj2) {
				Employee emp1 = (Employee) obj1;
				Employee emp2 = (Employee) obj2;
				int result = emp1.getFirstName().compareToIgnoreCase(emp2.getFirstName());
				System.out.println(emp1.id+" "+emp2.id+" "+result);
				return result;
			}
		});
		// Now the employeeList is sorted based on first name
		for(Employee employee : employeeList){
			System.out.println(employee.getFirstName());
		}
		
		System.out.println("Sort Based on LastName ");
		// sort by Last name
		Collections.sort(employeeList, new Comparator() {
			@Override
			public int compare(Object obj1, Object obj2) {
				Employee emp1 = (Employee) obj1;
				Employee emp2 = (Employee) obj2;
				int result = emp1.getLastName().compareToIgnoreCase(emp2.getLastName());
				System.out.println(emp1.id+" "+emp2.id+" "+result);
				return result;
			}
		});
		// Now the employeeList is sorted based on last name
	
		for(Employee employee : employeeList){
			System.out.println(employee.getLastName());
		}
	}
}
